#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
static char *program="";
void usage()
{
printf("%s:\n"
       "-d    dest ip address\n"
       "-p    dest port\n"
       "-h    help info\n",program);
       exit(-1);
}
char recv_buf[40960];
int main(int argc,char *argv[])
{
    struct sockaddr_in remote_addr;
    char *ip=NULL;
    char *port=NULL;
    int opt;
    program=argv[0];
    if(argc<2){
        usage();
    }
    while(-1!=(opt=getopt(argc,argv,"d:p:h"))){
        switch(opt){
            case 'd':
                ip=optarg;
                break;
            case 'p':
                port=optarg;
                break;
            case 'h':
            case '?':
                usage();
                break;
        }
    }
    if(NULL==ip||NULL==port){
        usage();
    }
    remote_addr.sin_family=AF_INET;
    remote_addr.sin_addr.s_addr=inet_addr(ip);
    remote_addr.sin_port=htons(atoi(port));
    int fd=socket(PF_INET,SOCK_STREAM,0);
    int ret=connect(fd,(struct sockaddr*)&remote_addr,sizeof(struct sockaddr_in));
    if(ret<0){
        printf("connect failed\n");
        return -1;
    }
    while(1){
        fd_set reads;
        FD_ZERO(&reads);
        FD_SET(fd,&reads);
        select(fd+1,&reads,NULL,NULL,NULL);
        if(FD_ISSET(fd,&reads)){
            int bytes=recv(fd,recv_buf,30000,0);
            printf("bytes=%d %s\n",bytes,strerror(errno));
            if(bytes<=0){
                break;
            }
        }
    }
}
